package com.aiden.cashbook.receiver

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.NotificationCompat
import cn.jpush.android.api.JPushInterface
import com.aiden.cashbook.MainActivity
import com.aiden.cashbook.R
import com.aiden.cashbook.utils.BitmapUtils
import com.aiden.cashbook.utils.LogUtils

class JPushBroadcastReceiver : BroadcastReceiver() {

    private val TAG = "JPushBroadcastReceiver"

    private var notificationManager: NotificationManager? = null

    override fun onReceive(context: Context?, intent: Intent?) {
        if (notificationManager == null) {
            notificationManager = context!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        }
        intent?.let {
            val bundle = it.extras
            when (intent.action) {
                JPushInterface.ACTION_REGISTRATION_ID -> LogUtils.d(TAG, "JPush用户注册成功")
                JPushInterface.ACTION_MESSAGE_RECEIVED -> {
                    LogUtils.d(TAG, "接受到推送下来的自定义消息")
                    notification(context, bundle)
                }
                JPushInterface.ACTION_NOTIFICATION_RECEIVED -> LogUtils.d(TAG, "接受到推送下来的通知")
                JPushInterface.ACTION_NOTIFICATION_OPENED -> LogUtils.d(TAG, "用户点击打开了通知")
                else -> LogUtils.d(TAG, "Unhandled intent - " + intent.action)
            }
        }

    }

    private fun notification(context: Context?, bundle: Bundle) {
        val title = bundle.getString(JPushInterface.EXTRA_TITLE)
        val msg = bundle.getString(JPushInterface.EXTRA_MESSAGE)
        val extras = bundle.getString(JPushInterface.EXTRA_EXTRA)

        val intent = Intent()
        when (extras) {
            "main" -> intent.setClass(context, MainActivity::class.java)
            "..." -> intent.setClass(context, MainActivity::class.java)
            else -> intent.setClass(context, MainActivity::class.java)
        }
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        val notificationCompat = context?.let {
            NotificationCompat.Builder(it)
        }?.run {
            setContentTitle(title)
                    .setAutoCancel(true)
                    .setContentText(msg)
                    .setTicker(msg)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapUtils.decodeBitmap(context, R.mipmap.ic_launcher, 72, 72))
                    .setContentIntent(pendingIntent)
                    .build()
        }

        val notifyId = System.currentTimeMillis()
        notificationManager?.notify(notifyId.toInt(), notificationCompat)
    }
}